package com.weiyuwqj.server.service.sys.impl;

import com.github.pagehelper.PageHelper;
import com.weiyuwqj.api.service.SysArticleService;
import com.weiyuwqj.server.service.base.BaseServiceImpl;
import com.weiyuwqj.api.common.PageList;
import com.weiyuwqj.api.common.Paginator;
import com.weiyuwqj.server.mapper.SysArticleMapper;
import com.weiyuwqj.server.mapper.SysPictureMapper;
import com.weiyuwqj.api.model.SysArticle;
import com.weiyuwqj.api.model.SysPicture;
import com.weiyuwqj.api.model.vo.ArticleVo;
import com.weiyuwqj.api.util.IpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by yandq on 15/11/7.
 */
@Service("articleService")
public class SysArticleServiceImpl extends BaseServiceImpl<SysArticle> implements SysArticleService {

    @Autowired
    SysArticleMapper sysArticleMapper;

    @Autowired
    SysPictureMapper pictureMapper;

    @Override
    public Integer deleteById(Integer articleId) {
        return sysArticleMapper.deleteById(articleId);
    }

    @Override
    public void insertArticle(SysArticle article,String pics) {
        article.setMark(0);
        sysArticleMapper.insertArticle(article);
        article.setId(article.getId());
        SysPicture pic = new SysPicture();
        pic.setAddip(IpUtils.get());
        pic.setAddtime(new Date());
        pic.setItemId(article.getId());
        pic.setPictureName(article.getId()+"");
        pic.setPictureUrl(pics);
        pic.setPicturePath(pics);
        pic.setSequence(1);
        pic.setPictureType(5);
        pictureMapper.addArticlePic(pic);
    }

    @Override
    public PageList getList(Paginator p,Map map) {
        PageHelper.startPage(p.getPageNum(), p.getPageSize());
        return new PageList(sysArticleMapper.getList(map));
    }

    /**
     * 获取最新的一条公告
     * @return 公告
     */
	@Override
	public List<SysArticle> getArticleIndex() {
		return sysArticleMapper.getArticleIndex();
	}

}
